//
//  CheekScreeningController.m
//  CheekiOS
//
//  Created by mac on 2017/7/10.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "CheekScreeningController.h"
#import "CheekScreeningCell.h"
#import "LiveViewController.h"
@interface CheekScreeningController ()<UITableViewDelegate, UITableViewDataSource, CheekScreeningCellDelegate>

@property (nonatomic, strong) NSArray *nameArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) NSString *CharmType;
//个人信息数据
@property (nonatomic, strong)UserpersionlInfoModel *model;


@end

@implementation CheekScreeningController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _CharmType = @"";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button setImage:[UIImage imageNamed:@"viedo_search_icon"] forState:UIControlStateNormal];
    [_button setTitle:@"搜索：所有人" forState:UIControlStateNormal];
    _button.titleLabel.font = FontP(17);
    [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _button.frame = CGRectMake(15, 0, KScreenWidth/2, 44);
    _button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    UIBarButtonItem *screeningitem = [[UIBarButtonItem alloc ] initWithCustomView:_button];
    self.navigationItem.leftBarButtonItem = screeningitem;
    
    
    
    
    
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"viedo_search_close_icon"] forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(KScreenWidth-15-30, 17, 30, 30);
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc ] initWithCustomView:backBtn];
    self.navigationItem.rightBarButtonItem = backItem;

    UIView *BgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    BgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    [self.view addSubview:BgView];
    
    
    
    CGFloat tableViewH;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
        NSLog(@"ipad");
        tableViewH =     KScreenHeight-49-100*SCREEN_WIDTH_RATIO47-75*SCREEN_WIDTH_RATIO47;

    }else{
        tableViewH =     KScreenHeight-49-200*SCREEN_WIDTH_RATIO47-75*SCREEN_WIDTH_RATIO47;
        NSLog(@"iphone or ipod");
    }

    
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(15, 30, KScreenWidth-30, tableViewH) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_tableView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight  | UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(10, 10)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _tableView.bounds;
        maskLayer.path = maskPath.CGPath;
        _tableView.layer.mask = maskLayer;
        [BgView addSubview:_tableView];
    }
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setBackgroundImage:[UIImage imageNamed:@"viedo_search_connect_icon"] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(buttonClickAction) forControlEvents:UIControlEventTouchUpInside];
    
    button.frame = CGRectMake(self.view.centerX-35*SCREEN_WIDTH_RATIO47, _tableView.bottom+35, 70*SCREEN_WIDTH_RATIO47, 70*SCREEN_WIDTH_RATIO47);
    
    [self.view addSubview:button];
    
    [self UserInfoRequestDate];
    
    
}


- (void)UserInfoRequestDate{
    
    [HcardHttpmanager getPersionalUserInfosuccess:^(id sucess) {
        self.model = [UserpersionlInfoModel mj_objectWithKeyValues:sucess[@"res"]];
        
    } failure:^(NSError *error) {
        
    }];
    
}


-(void)buttonClickAction{
    
    
    
    AVAuthorizationStatus videoStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(videoStatus == AVAuthorizationStatusRestricted || videoStatus == AVAuthorizationStatusDenied)
    {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"您没有相机使用权限,请到 设置->隐私->相机 中开启权限" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *confirAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVC addAction:confirAction];
        [self.navigationController presentViewController:alertVC animated:YES completion:nil];
        return;
    }
    AVAuthorizationStatus audioStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    if(audioStatus == AVAuthorizationStatusRestricted || audioStatus == AVAuthorizationStatusDenied)
    {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"您没有麦克风使用权限,请到 设置->隐私->麦克风 中开启权限" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *confirAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertVC addAction:confirAction];
        [self.navigationController presentViewController:alertVC animated:YES completion:nil];
        return;
    }
    
    
    
    if ([self.model.status isEqualToString:@"1"] && [self.model.photoStatus isEqualToString:@"1"]) {
        
        if ([_CharmType isEqualToString:@"1"] || [_CharmType isEqualToString:@"2"]) {
            
            LiveViewController *vc = [[LiveViewController alloc] init];
            
            
            if ([_CharmType isEqualToString:@"1"]) {
                vc.Sex = @"0";
            }else{
                vc.Sex = @"1";
            }
            [self presentViewController:vc animated:YES completion:nil];

        }else{
            
            LiveViewController *vc = [[LiveViewController alloc] init];
            
            vc.Sex = @"2";
            
            [self presentViewController:vc animated:YES completion:nil];
        }

        
    }else{
        if ([self.model.status isEqualToString:@"0"]) {
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"您的账号状态有误，请联系客服" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *confirAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alertVC addAction:confirAction];
            [self.navigationController presentViewController:alertVC animated:YES completion:nil];
        }else{
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"您头像未通过审核，请更改头像或联系客服" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *confirAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alertVC addAction:confirAction];
            [self.navigationController presentViewController:alertVC animated:YES completion:nil];
        }
        
    }

    
}

- (NSArray *)nameArray{
    
    if (!_nameArray) {
        NSArray * titleArr =  @[@"所有人",@"女士",@"男士"];
        NSArray * charm =  @[@"免费",@"2钻石/次",@"2钻石/次"];
        NSArray * imagePath = @[@"viedo_search_alll_icon",@"viedo_search_girl_icon",@"viedo_search_boy_icon"];
        NSMutableArray * dataArr = [NSMutableArray array];
        
        for (int i = 0; i < titleArr.count; i++) {
            UserEditEmotinalModel * model = [UserEditEmotinalModel new];
            model.title = titleArr[i];
            model.charm = charm[i];
            model.imagePath = imagePath[i];
            model.isSelect = NO;
            [dataArr addObject:model];
        }
        _nameArray = dataArr;
    }
    return _nameArray;
}

- (void)backBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)userEditEmotinalCell:(CheekScreeningCell *)cell clickBtn:(UIButton *)sender{
    NSLog(@"%ld",(long)sender.tag);
    for (UserEditEmotinalModel * model in self.nameArray) {
        if (model == cell.model) {
            model.isSelect = YES;
        }else{
            model.isSelect = NO;
        }
    }
    [self.tableView reloadData];
    
    [_button setTitle:[NSString stringWithFormat:@"搜索：%@",[_nameArray[sender.tag] valueForKey:@"title"]] forState:UIControlStateNormal];

    _CharmType = [NSString stringWithFormat:@"%ld",(long)sender.tag];
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.nameArray.count+1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CheekScreeningCell *cell = [CheekScreeningCell cellWithTableView:tableView indexPath:indexPath];
    if (indexPath.row != 3) {
        cell.model = self.nameArray[indexPath.row];

    }else{
        cell.model =self.nameArray[indexPath.row-1];
    }

    
    cell.delegate = self;
    

    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        return 200*SCREEN_WIDTH_RATIO47;
    }else{
        return 75*SCREEN_WIDTH_RATIO47;

    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
